package com.ylr.user.framework.service.impl;

import com.ylr.user.framework.entity.UserMessageEntity;
import com.ylr.user.framework.mapper.UserMessageMapper;
import com.ylr.user.framework.service.UserMessageService;
import com.ylr.user.common.bo.user_message.UserMessageBo;
import com.ylr.base.common.enums.CommonEnum;
import com.ylr.user.common.enums.UserMessageEnum;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylr.base.framework.service.SnowflakeService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-06-08 11:53:27
 * className: UserMessageServiceImpl
 * version: 1.0
 * description:
 */
@Service("userMessageService")
@AllArgsConstructor
public class UserMessageServiceImpl extends ServiceImpl<UserMessageMapper, UserMessageEntity> implements UserMessageService {

    private static final Logger log = LoggerFactory.getLogger(UserMessageServiceImpl.class);

    private final SnowflakeService snowflakeService;

    @Override
    public void saveUserMessage(UserMessageBo userMessageBo) {

        UserMessageEntity userMessage = this.getNewUserMessage(userMessageBo);
        // 保存用户消息
        baseMapper.insert(userMessage);
        log.info("保存用户消息完成");
    }

    /**
     * 用户消息参数转化
     * @param userMessageBo 用户消息参数
     * @return 用户消息实体对象
     */
    private UserMessageEntity getNewUserMessage(UserMessageBo userMessageBo) {
        Date date = new Date();

        UserMessageEntity userMessage = new UserMessageEntity();
        userMessage.setId(snowflakeService.getId())
                .setGetUserId(userMessageBo.getGetUserId())
                .setSendUserId(userMessageBo.getSendUserId())
                .setType(userMessageBo.getType())
                .setReadStatus(UserMessageEnum.READ_NO.getValue())
                .setTitle(userMessageBo.getTitle())
                .setMessage(userMessageBo.getMessage())
                .setDeleteStatus(CommonEnum.DELETE_NO.getValue())
                .setUpdateTime(date)
                .setCreateTime(date);
        return userMessage;
    }
}
